package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.SysMessage;
import com.moji.entity.dto.QueryMessageDto;
import com.moji.mapper.SysMessageMapper;
import com.moji.service.SysMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统消息表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-08-10
 */
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<SysMessage> queryByPage(QueryMessageDto dto) {
        LambdaQueryWrapper<SysMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getMarkAsRead()),SysMessage::getMarkAsRead,dto.getMarkAsRead());
        wrapper.eq(SysMessage::getReceiveUserId,MoJiCommonUtils.getUserCode());
        //创建时间倒叙
        wrapper.orderByDesc(SysMessage::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<SysMessage> sysMessages = list(wrapper);
        PageInfo<SysMessage> info = new PageInfo<>(sysMessages);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(List<Long> ids) {
        if(ObjectUtil.isNotEmpty(ids)){
            ids.forEach(id->{
                this.removeById(id);
            });
        }
        return true;
    }

    @Override
    public boolean add(SysMessage sysMessage) {
        sysMessage.setCreateId(MoJiCommonUtils.getUserCode());
        sysMessage.setUpdateId(MoJiCommonUtils.getUserCode());
        sysMessage.setCreateTime(new Date());
        sysMessage.setUpdateTime(new Date());
        return this.save(sysMessage);
    }

    @Override
    public boolean edit(SysMessage sysMessage) {
        SysMessage sysMessage1=this.getById(sysMessage.getId());
        BeanUtils.copyProperties(sysMessage,sysMessage1);
        sysMessage1.setUpdateId(MoJiCommonUtils.getUserCode());
        sysMessage1.setUpdateTime(new Date());
        return this.updateById(sysMessage1);
    }

    @Override
    public SysMessage detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }

    @Override
    public boolean isRead(List<Long> ids) {
        if(ObjectUtil.isNotEmpty(ids)){
            ids.forEach(id->{
                SysMessage sysMessage=this.getById(id);
                if(sysMessage==null){
                    throw new RuntimeException("消息不存在,请核查!");
                }
                if(sysMessage.getMarkAsRead()==0){
                    sysMessage.setMarkAsRead(1);
                }
                this.updateById(sysMessage);
            });
        }
        return true;
    }
}
